<?php
	require_once '../mysql_connection/for_m_goods.php';
	
	session_name('YourVisitID');
	session_start ();
	$error = array ();
	
	if (isset($_SESSION ['name'])) {
		$types = getAllType();
	} else {
		$error [] = 'You have not login!';
	}
	
	if (isset($_POST['submitted_search'])){
		$goods_search_info = array();
		if(isset($_POST['goods_type_search']) && isset($_POST['price_min']) && isset($_POST['price_max'])){
			$typeid_search = $_POST['goods_type_search'];
			$pricemin = $_POST['price_min'];
			$pricemax = $_POST['price_max'];
			$goods_searched = findGoods($typeid_search, $pricemin, $pricemax);
			
			foreach ($goods_searched as $goods_id){
				$goods_search_info[] = getGoodsInfo($goods_id);
			}
		}
	}
	if(isset($_POST['submitted_add'])){
		$typeid_add = $_POST['goods_type_add'];
		$goods_name_add = $_POST['name_add']; 
		$goods_price_add = $_POST['price_add'];
		$goods_illustrate_add = $_POST['illustrate_add'];
		$goods_left_add = $_POST['count_add'];
		
		$add_ok = mAddGoods($goods_name_add, $goods_price_add, $goods_left_add, $goods_illustrate_add, $typeid_add);
	}
	if(isset($_POST['submitted_newtype'])){
		$typename_new = $_POST['name_newtype'];
		$fatherid = $_POST['goods_type_newfather'];
		$newtype_ok = newType($fatherid, $typename_new);
	}
	mysqli_close($dbc);
?>

<?php 
include ('../include_htmls/manage_head.html');
?>

<?php
if (empty ( $error )) {
?>
		<form action="manage_goods.php" method="post">
		<div align="left">
			Types:
			    <select name="goods_type_search" size=1>
			    	<?php 
			    	foreach ($types as $onetype){
			    		echo "<option value=\"".$onetype[0]."\">".$onetype[2]."</option>";
			    	}
			    	?>
			    </select>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			Price:
			    <input type="text" name="price_min" size="5" maxlength="5" />
			    -
			    <input type="text" name="price_max" size="5" maxlength="5" />
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			
		    <input type="submit" name="submit_search" value="Search"/>
		    <input type="hidden" name="submitted_search" value="TRUE" />
		</div>
		</form>
<?php
	if (!empty($goods_search_info)){
?>
    <table >
        <?php 
        	foreach ($goods_search_info as $one_goods){
        ?>
        		
        		<tr>
    			    <th>
    			    	<?php 
    			    	echo "<img src=\"".$one_goods[8]."\" width=\"200\" height=\"150\" border=\"0\"/>";
    			    	?>
    			    </th>
    			    <th>
    			    	<table>
    			    		<tr>
    			    			<th>
    			    				<table cellspacing="0" cellpadding="3" border="1" align="left" width="700" height="100">
    			    					<tr>
    			    					    <th align="center">Name</th>
    			    					    <th align="center">Price</th>
    			    					    <th align="center">Score</th>
    			    					    <th align="center">Sold</th>
    			    					    <th align="center">Left</th>
    			    					    <th align="center">Type</th>
    			    					    <th align="center">Time</th>
    			    					</tr>
    			    					<tr>
    			    						<th align="center"><?php echo $one_goods[0];?></th>
    			    						<th align="center"><?php echo $one_goods[1];?></th>
    			    						<th align="center"><?php echo $one_goods[2];?></th>
    			    						<th align="center"><?php echo $one_goods[3];?></th>
    			    						<th align="center"><?php echo $one_goods[4];?></th>
    			    						<th align="center"><?php echo $one_goods[7];?></th>
    			    						<th align="center"><?php echo $one_goods[6];?></th>
    			    					</tr>
    			    				</table>
    			    			</th>
    			    		</tr>
    			    		<tr>
    			    			<th>
    			    				<table width="700">
    			    					<tr>
    			    		    			<th><?php echo "Illustrate:".$one_goods[5];?></th>
    			    		    			<th align="right"><input type="submit" name="change_goods" value="MakeChange"/></th>
    			    					</tr>
    			    				</table>
    			    		    </th>
    			    		</tr>
    			    	</table>
    			    </th>
  				</tr>
        <?php 
        	}
        ?>
	</table>

<?php
	}else if (isset($_POST['submitted_search'])){
		echo '<p align="center">There are no goods you want to search!</p>';
	}
?>
	<form action="manage_goods.php" method="post">
		<div align="left">
		    Name:
		        <input type="text" name="name_add" size="7" maxlength="20"/>
			SetType:
			    <select name="goods_type_add" size=1>
			    	<?php 
			    	foreach ($types as $onetype){
			    		echo "<option value=\"".$onetype[0]."\">".$onetype[2]."</option>";
			    	}
			    	?>
			    </select>
			Price:
			    <input type="text" name="price_add" size="5" maxlength="5" />
			TotalNum:
			    <input type="text" name="count_add" size="5" maxlength="5" />
			Illustrate:
			    <input type="text" name="illustrate_add" size="15" maxlength="40" />
			<input type="submit" name="submit_add" value="Add"/>
		    <input type="hidden" name="submitted_add" value="TRUE" />
		</div>
		</form>
		<?php 
			if($add_ok){
				echo '<p align="center">Add new goods successfully!</p>';
			}
		?>
		<form action="manage_goods.php" method="post">
		<div align="left">
		    New Type Name:
		        <input type="text" name="name_newtype" size="10" maxlength="10"/>
		    SetFatherType:
			    <select name="goods_type_newfather" size=1>
			    	<?php 
			    	echo "<option value=\"-1\">"."NULL"."</option>";
			    	foreach ($types as $onetype){
			    		echo "<option value=\"".$onetype[0]."\">".$onetype[2]."</option>";
			    	}
			    	?>
			    </select>
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
			&nbsp;
			<input type="submit" name="submit_newtype" value="NewType"/>
		    <input type="hidden" name="submitted_newtype" value="TRUE" />
		</div>
		</form>
		<?php 
			if($newtype_ok){
				echo '<p align="center">Add new Type successfully!</p>';
			}
		?>
<?php
} else {
	echo '<h1 align="center" id="mainhead">Error!</h1>
	<p align="center" class="error">The following error(s) occurred:<br />';
	foreach ( $error as $msg ) {
		echo " - $msg<br />\n";
	}
	echo '</p><p align="center" >Please login first.</p>';
	echo '<p align="center" ><a href="login.php"><img src="../images/manager_login.jpg" width="100" hight="25" border="0"/></a></p>';
}
?>

<?php
include ('../include_htmls/manage_foot.html');
?>
